<?php


namespace app\common\model;


use think\Model;

class CouponReceive extends Model
{
    protected $table = "maniac_coupon_receive";
    protected $createTime = "created_at";
    protected $updateTime = "updated_at";
    public $autoWriteTimestamp = "datetime";
    public function coupon(){
        return $this->hasOne('app\common\model\Coupon','id','coupon_id');
    }
    public function member(){
        return $this->hasOne('app\common\model\Members','id','member_id');
    }
    public function getMemberInfoAttr($value,$data){
        return Members::where("id",$data['member_id'])->field('id,nickname,image_uri')->find();
    }
    //开始时间
    public function getCouponPbeginAttr($value,$data){

        return date('Y-m-d H:i',Coupon::withTrashed()->where("id",$data['coupon_id'])->value('pbegin'));
    }
    //结束时间
    public function getCouponPendAttr($value,$data){
        return date('Y-m-d H:i',Coupon::withTrashed()->where("id",$data['coupon_id'])->value('pend'));
    }
    //优惠券类型
    public function getCouponTypeAttr($value,$data){
        $status = [0=>'满减劵',1=>'抵用券'];
        $type = Coupon::withTrashed()->where("id",$data['coupon_id'])->value('coupon_type');
        if ($type == ''){
            return  "无";
        }
        return $status[$type];
    }
    //用户获取数量
    public function getNumberAttr($value,$data){
        return self::where("coupon_id",$data['coupon_id'])->where('member_id',$data['member_id'])->where("status",$data['status'])->count('id');
    }
    //获取员工已发放数量
    public function getYgNumbersAttr($value,$data){
        return self::where("coupon_id",$data['coupon_id'])->where("member_id",$data['member_id'])->where('staff_id',$data['staff_id'])->count('id');
    }
    //获取员工核销数量
    public function getYgHxNumbersAttr($value,$data){
        return self::where("coupon_id",$data['coupon_id'])->where("member_id",$data['member_id'])->where('staff_id',$data['staff_id'])->where("status",$data['status'])->count('id');
    }
    //获取员工数量
    public function getYgNumberAttr($value,$data){
        return self::where("coupon_id",$data['coupon_id'])->where('staff_id',$data['staff_id'])->where("status",$data['status'])->count('id');
    }


}